package com.hy.study.bean;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * @author huyong
 * @date 2024 年 02 月 05 日 17:34: 08
 */
public class OrderBean implements WritableComparable<OrderBean> {
    /**
     * 订单ID
     */
    private int orderId;
    /**
     * 订单价格
     */
    private double price;

    public OrderBean(int orderId, double price) {
        this.orderId = orderId;
        this.price = price;
    }

    public OrderBean() {
    }

    public int getOrderId() {
        return orderId;
    }


    public void setOrderId(int orderId) {
        this.orderId = orderId;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return this.orderId + "\t" + this.price;
    }

    /**
     * 核心比较方法
     *
     * @param orderBean the object to be compared.
     *
     * @return
     */
    @Override
    public int compareTo(OrderBean orderBean) {
        // 先按照id的顺序排序如果相同在按照价格的降序排序
        int result;
        if (this.orderId > orderBean.orderId) {
            result = 1;
        } else if (this.orderId < orderBean.getOrderId()) {
            result = -1;
        } else {
            if (this.price > orderBean.getPrice()) {
                result = -1;
            } else if (this.price < orderBean.getPrice()) {
                result = 1;
            } else {
                result = 0;
            }
        }
        return result;
    }

    /**
     * 序列化
     *
     * @param out <code>DataOuput</code> to serialize this object into.
     *
     * @throws IOException
     */
    @Override
    public void write(DataOutput out) throws IOException {
        out.writeInt(this.orderId);
        out.writeDouble(this.price);
    }

    /**
     * 反序列化
     *
     * @param in <code>DataInput</code> to deseriablize this object from.
     *
     * @throws IOException
     */

    @Override
    public void readFields(DataInput in) throws IOException {
        this.orderId = in.readInt();
        this.price = in.readDouble();
    }
}
